//
//  MTSendRedpackInputView.swift
//  MT
//
//  Created by ooops on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTSendRedpackInputView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        backgroundColor = .white
        layer.cornerRadius = 8
    }
    
    var placeholder: String? {
        didSet {
            if placeholder != nil {
                let attribute: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(16),
                                                                 NSAttributedString.Key.foregroundColor : UIColor(hex: 0x5d5d5d)];
                contentTxf.attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: attribute)
            }
        }
    }
    
    var preTips: String? {
        didSet {
            preTipLabel.text = preTips
        }
    }
    
    var endTips: String? {
        didSet {
            endTipLabel.text = endTips
        }
    }
    
    var textValue: String? {
        get {
            return contentTxf.text
        }
    }
    
    var txfKeyboardType: UIKeyboardType = .default {
        didSet {
            contentTxf.keyboardType = txfKeyboardType
        }
    }
    
    var contentTextField: UITextField {
        get {
            return self.contentTxf
        }
    }
    
    private func setupUI() {
        addSubview(lineView)
        addSubview(preTipLabel)
        addSubview(contentTxf)
        addSubview(endTipLabel)
    }
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        preTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(lineView).offset(10)
            //make.bottom.equalTo(lineView.snp.top).offset(-8)
            make.centerY.equalToSuperview()
        }
        
        endTipLabel.snp.makeConstraints { (make) in
            make.right.equalTo(lineView).offset(-10).priority(.high)
            make.centerY.equalTo(preTipLabel)
        }
        
        contentTxf.snp.makeConstraints { (make) in
            make.centerY.equalTo(preTipLabel)
            make.left.equalTo(lineView).offset(preTipLabel.width + 10)
            make.right.equalTo(lineView).offset(-endTipLabel.width - 20)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        contentTxf.snp.updateConstraints { (make) in
            make.left.equalTo(lineView).offset(preTipLabel.width + 10)
            make.right.equalTo(lineView).offset(-endTipLabel.width - 20)
        }
    }
    
    
    private lazy var lineView: UIView = {
        let l = UIView()
        l.backgroundColor = UIColor(hex: 0xFFFFFF)
        return l
    }()
    
    private lazy var preTipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.sizeToFit()
        return l
    }()
    
    private lazy var endTipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.sizeToFit()
        return l
    }()
    
    private lazy var contentTxf: UITextField = {
        let txf = UITextField()
        txf.textColor = UIColor.text.x9b9b9b
        txf.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        txf.textAlignment = .right
        return txf
    }()
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
